//获取父地址的参数
function getQueryString(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.parent.location.search.substr(1).match(reg);
     if(r!=null)return  decodeURI(r[2]); return null;
}

//ajax请求封装
var myAjax = {
	get : function(options){
		$.ajax({ 
			type:'get',
			url: options.url?options.url : "",
			data: options.data?options.data:"",
			dataType: options.dataType?options.dataType:"json",
			contentType : options.contentType,
			async : options.async==undefined?true : options.async,
			success: typeof options.success == 'function'?options.success : function(){},	
			error : typeof options.error == 'function'?options.error: function(){}	
		});	
	},
	
	post : function(options){
		$.ajax({ 
			type:'post',
			url: options.url?options.url : "",
			data: options.data?options.data:"",
			dataType: options.dataType?options.dataType:"json",
			contentType : options.contentType,
			async : options.async==undefined?true : options.async,
			success: typeof options.success == 'function'?options.success : function(){},	
			error : typeof options.error == 'function'?options.error: function(){}		
		});	
	}
}


//创建验证码
function createCode(codeEl){
	 if(codeEl.indexOf('#')==0 || codeEl.indexOf('.')==0){
		 codeEl = codeEl.substring(1,codeEl.length);
	 }
	 var code = "";
	 var codeLength = 5; //验证码的长度
	 var checkCode = document.getElementById(codeEl);
	 var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
	      'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
	      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //所有候选组成验证码的字符，当然也可以用中文的
	 for(var i = 0; i < codeLength; i++) {
		  var charNum = Math.floor(Math.random() * 52);
		  code += codeChars[charNum];
	 }
	 if(checkCode) {
		  checkCode.className = "code";
		  checkCode.innerHTML = code;
	 }
	 return code;
}

//校验验证码
function checkCode(codeEl,inputCodeEl){	
	var result = true;

	var strMsg = "";
	var obj = $(codeEl).html();
	var input = $(inputCodeEl).val();
	
	if(!input){
		result = false;
		strMsg = "请填写验证码！";
	}else{
		if(obj.toLowerCase()!=input.toLowerCase()){
			result = false;
			strMsg = "验证码错误！";
		}
	}
	if(strMsg != ""){
		$.alert(strMsg);
	}
	
	return result;
}



var projectPath = "";
function basePath() {
	// 获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
	var curWwwPath = window.document.location.href;
	// 获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8080
	var localhostPath = curWwwPath.substring(0, pos);
	// 获取带"/"的项目名，如：/ems
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	// 获取项目的basePath http://localhost:8080/ems/
	var basePath = localhostPath + projectName + "/";
	return basePath;
};
projectPath = basePath();

// 表单数据转换成json(去除空字符)
$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();

	$(this).find('tbody').find('[name]').each(function() {
		if($(this).attr('no') == 'N')
			return true;
		
		if ($(this).val() != '') {
			if ($(this).attr('type') == 'checkbox') {
				if ($(this).is(':checked')) {
					o[$(this).attr('name')] = "1";
				} else {
					o[$(this).attr('name')] = "0";
				}
			} else if ($(this).attr('type') == 'radio') {
				var val=$('input:radio[name="'+$(this).attr('name')+'"]:checked').val();
				o[$(this).attr('name')] = val;
			} else {
				if (o[$(this).attr('name')]) {
					if (!o[$(this).attr('name')].push) {
						o[$(this).attr('name')] = [ o[$(this).attr('name')] ];
					}
					o[$(this).attr('name')].push($(this).val() || '');
				} else {
					o[$(this).attr('name')] = $(this).val() || '';
				}
			}
		}
	});
	return o;
};

$.fn.serializeObject1 = function(){
	var o = {};
	var a = this.serializeArray();

	$(this).find('[name]').each(function() {
		if ($(this).val() != '') {
			if ($(this).attr('type') == 'checkbox') {
				if ($(this).is(':checked')) {
					o[$(this).attr('name')] = "1";
				} else {
					o[$(this).attr('name')] = "0";
				}
			} else if ($(this).attr('type') == 'radio') {
				var val=$('input:radio[name="'+$(this).attr('name')+'"]:checked').val();
				o[$(this).attr('name')] = val;
			} else {
				if (o[$(this).attr('name')]) {
					if (!o[$(this).attr('name')].push) {
						o[$(this).attr('name')] = [ o[$(this).attr('name')] ];
					}
					o[$(this).attr('name')].push($(this).val() || '');
				} else {
					o[$(this).attr('name')] = $(this).val() || '';
				}
			}
		}
	});
	return o;
};
$.fn.serializeObjectArray = function() {
	var a = [];
	$(this).find('tbody').find('tr').each(function() {
		var o = {};
		$(this).find('[name]').each(function() {
			if ($(this).val() != '') {
				o[$(this).attr('name')] = $(this).val();
			}

			if ($(this).attr('type') == 'checkbox') {
				if ($(this).is(':checked')) {
					o[$(this).attr('name')] = "1";
				} else {
					o[$(this).attr('name')] = "0";
				}
			}
		})

		a.push(o);
	});

	return a;
}

// 获取url中的参数
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); // 匹配目标参数
	if (r != null)
		return unescape(r[2]);
	return null; // 返回参数值
}

// 定义页面的modal大小
function setModalSize() {
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#modalW').width(documentW * 0.6);
	$('#modalH').height(documentH * 0.7);
}

// 获取modal高度
function getModalHeight() {
	return $('#modalH').height();
}

Date.prototype.format = function(fmt) { 
    var o = { 
       "M+" : this.getMonth()+1,                 //月份 
       "d+" : this.getDate(),                    //日 
       "h+" : this.getHours(),                   //小时 
       "m+" : this.getMinutes(),                 //分 
       "s+" : this.getSeconds(),                 //秒 
       "q+" : Math.floor((this.getMonth()+3)/3), //季度 
       "S"  : this.getMilliseconds()             //毫秒 
   }; 
   if(/(y+)/.test(fmt)) {
           fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
   }
    for(var k in o) {
       if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
   return fmt; 
}


function formatDate(date,format)
{
	if(!date)
	{
		return '';
	}
	else if(typeof(date) == "string")
	{
		if("null"==date)
		{
			return '';
		}
		return date;
	}
	else if(typeof(date) == "undefined" || "undefined"==date)
	{
		return '';
	}
	else if(typeof(date) == "object")
	{
		var year = date.year + 1900;
		var month = (date.month + 1)>=10 ? (date.month + 1):"0"+(date.month + 1);
		var day = date.date>=10 ? date.date:"0"+date.date;
		var hours = date.hours>=10 ? date.hours:"0"+date.hours;
		var minutes = date.minutes>=10 ? date.minutes:"0"+date.minutes;
		var seconds = date.seconds>=10 ? date.seconds:"0"+date.seconds;
		if(format && "hh:mi:ss" == format)
		{
			return hours+":"+minutes+":"+seconds
		}
		else if(format && "yyyy年mm月dd日" == format)
		{
			return year+"年"+month+"月"+day+"日"
		}
		else if(format && "yyyy-mm-dd" == format)
		{
			return year+"-"+month+"-"+day
		}
		else if(format && "yyyy-mm-dd hh:mi:ss" == format)
		{
			return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds
		}
		else if(format && "yyyy-mm-dd hh:mi" == format)
		{
			return year+"-"+month+"-"+day+" "+hours+":"+minutes
		}
		else if(format && "yyyy-mm-dd hh" == format)
		{
			return year+"-"+month+"-"+day+" "+hours
		}
		else if(format && "yyyy/mm/dd" == format)
		{
			return year+"/"+month+"/"+day
		}
		else if(format && "yyyy/mm/dd hh:mi:ss" == format)
		{
			return year+"/"+month+"/"+day+" "+hours+":"+minutes+":"+seconds
		}
		else if(format && "yyyy/mm/dd hh:mi" == format)
		{
			return year+"/"+month+"/"+day+" "+hours+":"+minutes
		}
		return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
	}
	return date;
}

